#include "CCSkin.h"
#include "CCSlot.h"



CCSkin::CCSkin() {
    
}

CCSkin::~CCSkin() {
    for (auto iter:m_slotMap) {
        iter.second->release();
    }
    m_slotMap.clear();
}

CCSkin* CCSkin::create() {
    CCSkin* s = new CCSkin();
    return (CCSkin*)s->autorelease();
}

void CCSkin::addSlot(const char* slotName,CCAttachment* attachment) {
    auto iter=m_slotMap.find(slotName);
    if (iter==m_slotMap.end()) {
        Array* ary=Array::create();
        ary->retain();
        iter=m_slotMap.insert(make_pair(slotName,ary)).first;
    }
    iter->second->addObject(attachment);
}

CCAttachment* CCSkin::getAttachment(const char* slotName,const char* attachmentName) {
    auto iter=m_slotMap.find(slotName);
    if(iter!=m_slotMap.end()) {
        Array* ary=iter->second;
        for (unsigned int i = 0;i < ary->count();i++) {
            CCAttachment* attachment=(CCAttachment*)ary->objectAtIndex(i);
            if (!strcmp(attachment->getName(), attachmentName)) {  //TODO OPT
                return attachment;
            }
        }
    }
    return NULL;
}


